package com.uifp.gradebook;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;

import com.uifp.gradebook.adaptors.TabsPagerAdapter;
import com.uifp.gradebook.business_objects.Class;

public class ViewSelectedClassActivity extends FragmentActivity implements ActionBar.TabListener {

	private ViewPager viewPager;
	private TabsPagerAdapter mAdapter;
	private ActionBar actionBar;
	private int classID;
	public final static String CLASS_ID = "com.uifp.CLASS";
	
	// Tab titles
	private String[] tabs = { "Actual", "Projected", "Target" };
	
	public ViewSelectedClassActivity() {

	}

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_view_selected_class);
	    Log.d("MyApp","VSC activity created");
	    // Get the message from the intent
	 	Intent myIntent = getIntent();
	 	this.classID = myIntent.getIntExtra(CLASS_ID, 0);
	 	
	 	DataBaseHelper dbHelper = new DataBaseHelper(this);
	 	Class aClass = dbHelper.getClassByID(classID);
	 	
	 	TextView classLabel = (TextView) findViewById(R.id.vsc_class_title);
	 	classLabel.setText(aClass.getName());
	 	
	 	TextView profLabel = (TextView) findViewById(R.id.vsc_professor);
	 	profLabel.setText(aClass.getProf());
	 	
	 	//Initialize a Bundle to send the classID to the fragments.
	 	Bundle bundle = new Bundle();
	 	bundle.putInt("CLASS_ID", classID);

	    // Initialization
	    viewPager = (ViewPager) findViewById(R.id.pager);
	    mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), bundle);
	    viewPager.setAdapter(mAdapter);

	    actionBar = getActionBar();
	    
	    // enable navigation to home page from actionBar icon
	    actionBar.setHomeButtonEnabled(true);
	    
	    //sets the title in the actionBar
	    actionBar.setTitle("Course");
	    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

	    // Adding Tabs
	    for (String tab_name : tabs) {
	        actionBar.addTab(actionBar.newTab().setText(tab_name)
	                .setTabListener(this));
	    }
		
		// on swiping the viewpager make respective tab selected
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
		 
		    @Override
		    public void onPageSelected(int position) {
		        // on changing the page
		        // make respected tab selected
		        actionBar.setSelectedNavigationItem(position);
		    }
		 
		    @Override
		    public void onPageScrolled(int arg0, float arg1, int arg2) {
		    }
		 
		    @Override
		    public void onPageScrollStateChanged(int arg0) {
		    }
		});
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());	
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

		
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    // Respond to the action bar's Up/Home button
	    case android.R.id.home:
	    	Intent myIntent = new Intent(ViewSelectedClassActivity.this, MainActivity.class);
	    	NavUtils.navigateUpTo(ViewSelectedClassActivity.this, myIntent);
	        return true;
	    }
	    return super.onOptionsItemSelected(item);
	}

}



